//
//  SinaHomeCell.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/27.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaHomeCell: UITableViewCell {
    
    //底部视图顶部约束记录
    var bottomViewTopCons: Constraint?
    
    //定义一个属性SinaStatusViewModel
    var statusViewModel: SinaStatusViewModel?{
        didSet{
            //给原创微博赋值
            originalView.statusViewModel = statusViewModel
            
//            let picUrls = statusViewModel?.statusModel?.pic_urls
//            print(picUrls)
              //卸载约束
            bottomViewTopCons?.uninstall()
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                // 隐藏
                retweetView.isHidden = true
                
            }else {
                //赋值
                retweetView.statusViewModel = statusViewModel
                // 有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.isHidden = false
            }
            // 底部视图赋值
            bottomView.statusViewModel = statusViewModel

        }
}

    //MARK: - 重写 initStyle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //1.添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //2.添加约束
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
//            make.height.equalTo(50)
        }
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
//            make.height.equalTo(50)
        }
        bottomView.snp_makeConstraints { (make) in
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)           
            make.bottom.equalTo(contentView)
        }
    }
    
    //MARK: - 懒加载控件
    //原创微博
    private lazy var originalView: SinaHomeOriginalView = SinaHomeOriginalView()
    //转发微博
    private lazy var retweetView: SinaStatusRetweetView = SinaStatusRetweetView()
    //底部视图
    private lazy var bottomView: SinaStatusBottomView = SinaStatusBottomView()
}
